<template>
  <div>
  <!-- 按钮和搜索栏容器 -->
  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
    <div style="display: flex; gap: 5px;">
      <el-button v-show="returnButton" @click="returnLists()"><el-icon><Back /></el-icon></el-button>
    </div>
    <!-- 搜索栏 -->
    <div>
      <el-input
          v-model="searchname"
          placeholder="请输入会议室名称、客户名"
          style="width: 250px;"
          @keyup.enter="search()"
      >
        <template #append>
          <el-button @click="search()"><el-icon><Search /></el-icon></el-button>
        </template>
      </el-input>
    </div>
  </div>

  <el-table id="mytable" :data="filteredData" stripe border style="width: 100%">
    <el-table-column fixed prop="id" label="订单id" width="80" align="center"/>
    <el-table-column prop="roomName" label="会议室名称" width="150" align="center"/>
    <el-table-column prop="customerUsername" label="客户名称" align="center"/>
    <el-table-column prop="roomType" label="会议室类型" align="center">
      <template #default="scope">
           {{ scope.row.type==='classroom' ? '教室型' : '圆桌型' }}
      </template>
    </el-table-column>
    <el-table-column prop="attendeeCount" label="参会人数" align="center"/>
    <el-table-column prop="equipmentNeeds" label="所需设备" align="center">
      <template #default="scope">
           {{ scope.row.equipmentNeeds || '无' }}
      </template>
    </el-table-column>
    <el-table-column prop="amount" label="订单总金额" align="center"/>
    <el-table-column prop="status" label="订单状态" align="center">
      <template #default="scope">
         {{ formatStatus(scope.row.status) }}
      </template>
    </el-table-column>
    <el-table-column label="会议开始时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.startTime) }}
       </template>
    </el-table-column>
    <el-table-column label="会议结束时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.endTime) }}
       </template>
    </el-table-column>
    <el-table-column label="支付完成时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.payTime) }}
       </template>
    </el-table-column>
    <el-table-column label="订单创建时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.createTime) }}
       </template>
    </el-table-column>
    <el-table-column label="操作" width="180" align="center">
      <template v-slot="scope">
        <el-button v-if="scope.row.status==='cancelled'" link @click="handleViewCancelDetail(scope.row)" type="primary" size="big">查看退款详情</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <!-- 分页组件 -->
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="totalCount"
  >
  </el-pagination>
  <!-- 取消详情对话框 -->
  <el-dialog v-model="cancelDetailVisible" title="订单取消详情" width="40%">
    <el-card class="box-card">
  <el-table :data="[cancelForm]" border style="width: 100%;" size="big">
    <el-table-column label="订单总金额" width="120">
      <template #default="{ row }">
        <span>{{ row.amount }}</span>
      </template>
    </el-table-column>
    <el-table-column label="实际退款金额" width="120">
      <template #default="{ row }">
        <span>{{ row.refundAmount || '无' }}</span>
      </template>
    </el-table-column>
    <el-table-column label="取消时间" width="180">
      <template #default="{ row }">
        <span>{{ formatCreateTime(row.cancelTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="取消原因" width="200">
      <template #default="{ row }">
        <span>{{ row.cancelReason }}</span>
      </template>
    </el-table-column>
  </el-table>
</el-card>
    <template #footer>
      <el-button @click="cancelDetailVisible = false">关闭</el-button>
    </template>
  </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { Search } from '@element-plus/icons-vue';
import { Back} from "@element-plus/icons-vue";
export default {
  name: "LostAndFoundList",
  data() {
    return {
      listData: [],
      filteredData: [],
      searchname: '',
      returnButton:false,
      currentPage:1,
      pageSize:10,
      totalCount:0,  
      cancelDetailVisible: false,  
      cancelForm:{},
    }
  },
  components :{
     Search,
     Back,
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    handleCurrentChange(page){
        this.currentPage=page;
        if(!this.searchname){
        this.fetchData();
        }else{
          this.search();
        }
    },
    fetchData(){
        axios.get("/admin/reservation",{
          params:{
            pageNum:this.currentPage,
            pageSize:this.pageSize,
          }
        })
        .then(res=>{
        
          this.listData=res.data.data.records;
          this.totalCount=res.data.data.total;
          if (Array.isArray(this.listData)) {
            this.filteredData = this.listData;
          } else {
              console.error('加载错误');
          }
        }).catch(err=>{
          this.$message.error(err.data.code||"获取列表失败");
        })
    },
    search() {
      this.returnButton=true;
      axios.get('/admin/reservation',{
          params:{
            keyword:this.searchname,
            pageNum:this.currentPage,
            pageSize:this.pageSize,
          }
        })
      .then(res =>{
        if(res.data.code===200){
          this.filteredData=res.data.data.records;
          this.totalCount=res.data.data.total;
          this.$message.success("查询成功");
        }else {
          this.$message.error(res.data.msg||"查询失败");
        }
      })
    },
    returnLists(){
      this.returnButton=false;
      this.searchname=null;
      this.currentPage=1;
      this.fetchData();
    },
    goBackToList() {
      this.fetchData();
    },
    formatCreateTime(time){
      if(time){
        return time.replace('T',' ');
      }
      return '无'
    },
    formatStatus(status){
      if(status==='paid'){
        return '已支付';
      }else if(status==='unpaid'){
        return '未支付';
      }else if(status==='under_review'){
        return '待审核';
      }else if(status==='cancelled'){
        return '已取消';
      }else if(status==='used'){
        return '已使用';
      }
    },
    handleViewCancelDetail(row) {
      axios.get('/admin/reservation/'+row.id).then(res=>{
        if(res.data.code===200){
          this.cancelForm=res.data.data;
          this.cancelDetailVisible = true;
        }else {
          this.$message.error(this.data.msg || '加载失败！')
        }
      })
      
      
    },
  },
}

</script>

<style scoped>
.clicked-s {
  background-color: green;
  color: white;
}
</style>